package com.example.controller;

import com.example.entity.ShoppingCarts;
import com.example.entity.SysUser;
import com.example.entity.vo.ShopCartsVo;
import com.example.service.IShopKeeperService;
import com.example.service.IShoppingCartsService;
import com.example.ulit.UsrIdByToken;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2024-09-13
 */
@RestController
@RequestMapping("/uni-app/shopping-carts")
public class ShoppingCartsController {
    @Resource
    IShoppingCartsService shoppingCartsService;

    @Resource
    UsrIdByToken usrIdByToken;

    @RequestMapping("/list")
    public Object selectUsrId(String token){
        Map<String,Object> objectMap = new HashMap<>();
        objectMap.put("shoppingCarts",shoppingCartsService.selectAllByUsrId(Integer.parseInt(usrIdByToken.getStringRedisTemplate(token).toString())));
        objectMap.put("shopKeeper",shoppingCartsService.selectAllByKeeperIdShopCartsVo(Integer.parseInt(usrIdByToken.getStringRedisTemplate(token).toString())));
        return objectMap;
    }

    @RequestMapping("/goodsShopCount")
    public Object selectByUsrId(String token){
        return shoppingCartsService.searchAllByUsrIdShopCartsVo(Integer.parseInt(usrIdByToken.getStringRedisTemplate(token).toString()));
    }

    @RequestMapping("/insert")
    public Object insertUsr(ShoppingCarts shoppingCarts){
        System.out.println(shoppingCarts.getUsrId());
        int numCount = 0;
        ShopCartsVo shopCartsVo = shoppingCartsService.selectAllByGoodsId(shoppingCarts.getGoodsId(),shoppingCarts.getUsrId());
        if(shopCartsVo.getNumCount() != 0){
            numCount = shoppingCarts.getShopNumber() + shopCartsVo.getShopNumber();
            shoppingCarts.setShopNumber(numCount);
            shoppingCarts.setShopId(shopCartsVo.getShopId());
            return shoppingCartsService.updateById(shoppingCarts);
        }else{
            return shoppingCartsService.save(shoppingCarts);
        }

    }

    @RequestMapping("/update")
    public Boolean updateUsr(@RequestParam("shopFig") String shopFig,
                             @RequestParam("shopId") Integer shopId,
                             @RequestParam("shopNumber")Integer shopNumber){
        return shoppingCartsService.updateGoodsId(shopFig,shopId,shopNumber);
    }

    @RequestMapping("/delete")
    public Boolean deleteShopId(@RequestParam("shopId") String shopId){
        return shoppingCartsService.removeById(shopId);
    }

    @RequestMapping("/checkOut")
    public Boolean checkOutShop(@RequestParam("token") String token){
        return shoppingCartsService.checkOutShopByUsrId(Integer.parseInt(usrIdByToken.getStringRedisTemplate(token).toString()));
    }
}
